﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Messaging;

namespace ProjectSpam
{
    public partial class Recover : Form
    {
        int lastpicked;
        List<BackUpInfo> listBackups;
        //List<DateTime> listDates;

        const string sendQueue = ".\\private$\\projectSpamQueueRecover";

        public Recover(string path)
        {
            InitializeComponent();
            textBox_path.Text = path;

            Packer newp = new Packer();
            listBackups = newp.getBackUps();

            //SendMessageToQueue("GetRecoveryInformation");

            DateTime start = DateTime.Now;
            Boolean noAnswer = true;
            TimeSpan elapsed = DateTime.Now - start;

            while (elapsed.Seconds < 1 && noAnswer)
            {
                elapsed = DateTime.Now - start;
            }

           // if (noAnswer)
            //    MessageBox.Show("Unable to communicate with Backup/recovery Service");


            //present data
            //show factors

            this.listView1.Clear();
            this.listView1.Groups.Add(new ListViewGroup("Backups: ", HorizontalAlignment.Left));

            //ListViewItem newItem = new ListViewItem("Factores: ");
            for (int i = 0; i < listBackups.Count(); i++)
            {
                ListViewItem newItem = new ListViewItem(listBackups.ElementAt(i).Name + " from " + listBackups.ElementAt(i).Created);
                listView1.Items.Add(newItem);
                listView1.Items[i].Group = listView1.Groups[0];

            }



        }


        private static void SendMessageToQueue(string command)
        {
            // check if queue exists, if not create it

            MessageQueue msMq = null;
            try
            {
                if (!MessageQueue.Exists(sendQueue))
                {
                    msMq = MessageQueue.Create(sendQueue);
                }
                else
                {
                    msMq = new MessageQueue(sendQueue);
                }
            }
            catch (MessageQueueException e)
            {

            }

            try
            {
                msMq.Send("Sending data to MSMQ at " + DateTime.Now.ToString());

                System.Messaging.Message newMessage = new System.Messaging.Message(command);
                msMq.Send(newMessage);
                // msMq.Send(files);
            }
            catch (MessageQueueException ee)
            {
                Console.Write(ee.ToString());
            }
            catch (Exception eee)
            {
                Console.Write(eee.ToString());
            }
            finally
            {
                msMq.Close();
            }


        }



        private static bool ReceiveMessageFromQueue(string queueName)
        {

            MessageQueue msMq = msMq = new MessageQueue(queueName);

            try
            {
                // msMq.Formatter = new XmlMessageFormatter(new Type[] {typeof(string)});
                msMq.Formatter = new XmlMessageFormatter(new Type[] { typeof(List<string>) });
                List<string> message = (List<string>)msMq.Receive().Body;



            }
            catch (MessageQueueException ee)
            {
                Console.Write(ee.ToString());
                return false;
            }
            catch (Exception eee)
            {
                Console.Write(eee.ToString());
                return false;
            }
            finally
            {
                msMq.Close();
            }
            Console.WriteLine("Message received ......");
            return true;
        }



        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListView lv = (ListView)sender;
            try
            {
                string selectedString = lv.SelectedItems[0].ToString();



                //search for the index
                for (int i = 0; i < listBackups.Count; i++)
                {
                    //textBox1.Text = "" + string.Compare(selected, facList.ElementAt(i));
                    if (selectedString.IndexOf(listBackups.ElementAt(i).Name + " from ") >= 0)
                    {
                        //found
                        lastpicked = i;

                    }
                }
            }
            catch (Exception exp)
            {
            }
        }



        private void Button_Select_Click(object sender, EventArgs e)
        {
            //get name of the backup using the index
            //ask service for List of files for that backup

            SendMessageToQueue("GetBackupInformation");

            DateTime start = DateTime.Now;
            Boolean noAnswer = true;
            TimeSpan elapsed = DateTime.Now - start;

            while (elapsed.Seconds < 1 && noAnswer)
            {
                elapsed = DateTime.Now - start;
            }

            if (noAnswer)
                MessageBox.Show("Unable to communicate with Backup/recovery Service");


            //convert list to tree
            //TreeNode node = ListToTree(listBackups.ElementAt(lastpicked).Files);

            System.Threading.Thread t = new System.Threading.Thread
                (new System.Threading.ParameterizedThreadStart(openRecovery));
            t.SetApartmentState(System.Threading.ApartmentState.STA);
            t.Start(listBackups.ElementAt(lastpicked));

            this.Close();
        }


        public static void openRecovery(object model)
        {
            Application.Run(new Recover_Tree(model));
        }

        private void Button_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }





       
    }
        

}
